<!DOCTYPE html>
<html lang="en">

<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <meta http-equiv="X-UA-Compatible" content="ie=edge" />
  <link rel="stylesheet" href="css/reset.css" />
  <link rel="stylesheet" href="css/main.css" />
  <script type="text/javascript" src="./lib/jquery-1.12.4.min.js"></script>
  <script type="text/javascript" src="./lib/jquery-ui.min.js"></script>
  <script type="text/javascript" src="./lib/jquery.mousewheel.js"></script>
  <script type="text/javascript" src="./lib/scroll.js"></script>
  <title>聊天机器人</title>
</head>

<body>
  <div class="wrap">
    <!-- 头部 Header 区域 -->
    <div class="header">
      <h3>小思同学</h3>
      <img src="img/person01.png" alt="icon" />
    </div>
    <!-- 中间 聊天内容区域 -->
    <div class="main">
      <ul class="talk_list" style="top: 0px;">
        <li class="left_word">
          <img src="img/person01.png" />
          <span>你好</span>
        </li>
        <li class="right_word">
          <img src="img/person02.png" />
          <span>你好哦</span>
        </li>
      </ul>
      <!-- 右侧滚动条 -->
      <div class="drag_bar" style="display: none;">
        <div
             class="drager ui-draggable ui-draggable-handle"
             style="display: none; height: 412.628px;"></div>
      </div>
    </div>
    <!-- 底部 消息编辑区域 -->
    <div class="footer">
      <img src="img/person02.png" alt="icon" />
      <!-- 输入框 -->
      <input type="text" placeholder="说的什么吧..." class="input_txt" />
      <!-- 发送按钮 -->
      <input type="button" value="发 送" class="input_sub" />
    </div>
  </div>

  <!-- 导入axios -->
  <script src="./lib/axios.js"></script>
  <script>
    //获取
    const input_txt = document.querySelector('.input_txt')
    const input_sub = document.querySelector('.input_sub')
    //回车的时候
    input_txt.addEventListener('keyup',function(e){
      //判断按下的键
      if(e.key === 'Enter'){
        //发送请求
        chat()
      }
    })
    //点击的时候
    input_sub.addEventListener('click',function(){
      //发送请求
      chat()
    })
    //执行事件，发送请求函数
    const talk_list = document.querySelector('.talk_list')
    const chat = function(){
      //获取数值
      const value = input_txt.value.trim()
      if(!value) return
      const list = document.createElement('li')
      list.innerHTML=`
      <img src="img/person02.png" />
          <span>${value}</span>
      `
      list.classList.add('right_word')
      talk_list.append(list)
      input_txt.value=''
      //发送
      axios.get(`http://ajax-api.itheima.net/api/robot?spoken=${value}`)
      .then(res=>{
        const list = document.createElement('li')
        list.innerHTML=`
        <img src="img/person01.png" />
            <span>${res.data.data.info.text}</span>
        `
        list.classList.add('left_word')
        talk_list.append(list)


        //调用滚动
        resetui()
      })
    }
  </script>
</body>

</html>